package com.yuan.controller;

import com.yuan.bean.User;
import com.yuan.bean.UserLogin;
import com.yuan.service.UserLoginService;
import com.yuan.service.UserServier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {


    @Autowired
    UserLoginService userLoginService;

//登录页
    @GetMapping(value={"/","/login","/Login.html"})
    public String loginPage(){
        return "Login";
    }

//    登录成功重定向到desk.html
    @PostMapping("/login")
    public String main(@RequestParam("userName") String userName,@RequestParam("password")String password, HttpSession session, Model model){
        List<UserLogin> userLogins = userLoginService.selectAll();
        boolean isLogin=false;
        boolean isManager=false;
        for(UserLogin userLogin:userLogins){
            if(userLogin.getUserName().equals(userName) && userLogin.getPassword().equals(password)){
                    isLogin=true;
                    isManager=userLogin.isManager();
                    session.setAttribute("User",userLogin);
            }
        }
        if(isLogin){
            if(isManager){
                return "redirect:/homepage.html";
            }
            return "redirect:/desk.html";
        }else{
              model.addAttribute("msg","账号或密码错误");
//              回到登录页
            return "Login";
        }
    }

    @GetMapping("/desk.html")
    public String mainPage(HttpSession session,Model model){
        return "desk";
    }


    //添加用户
    @PostMapping("/adduser")
    public String addUser(@RequestParam("userName") String userName,@RequestParam("password") String password
                          ,@RequestParam("rePassword")String rePassword, Model model) {
        boolean notSame = false;
        List<UserLogin> userLogins = userLoginService.selectAll();
        for (UserLogin userLogin : userLogins) {
            if (userLogin.getUserName().equals(userName)) {
                notSame = false;
            } else {
                notSame = true;
            }
        }
        if (notSame) {
            if (password.equals(rePassword)) {
                UserLogin userLogin = new UserLogin(userName, password, false);
                boolean b = userLoginService.addUser(userLogin);
                System.out.println(b);
                if (b) {
//                    model.addAttribute("msg", "注册成功！");
                    return "redirect:/Login.html";
                }
                model.addAttribute("msg", "注册失败！");
                return "redirect:/Login.html";
            } else {
                model.addAttribute("msg", "两次密码不相同");
                return "redirect:/Login.html";
            }
        }else{
            model.addAttribute("msg", "用户名已有");
            return "redirect:/Login.html";
        }
    }

    //------------------------
    @GetMapping("/form")
    public String selectAllp(Model model){
        List<UserLogin> userLogins = userLoginService.selectAll();
        model.addAttribute("users",userLogins);
        return "form";
    }

    //-----------------------------------------------
}
